package com.daykw.douyin.task;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.alibaba.fastjson.JSONObject;
import com.daykw.douyin.config.NutzDaoConfig;
import com.daykw.douyin.utils.SpringContextHolder;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;

public class StarAuthInfoThread extends Thread {

	private static Logger logger = Logger.getLogger(StarAuthInfoThread.class);
	private NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

	@Override
	public void run() {

		try {

			Record r = dao.fetch("t_star_auth_info", Cnd.where("status", "=", 0));

			Map<String, String> headers = new HashMap<String, String>();
			String ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36";

			Record c = dao.fetch("t_star_cookie", Cnd.where("id", "=", 1));
			String cookie = c.getString("cookie");
			headers.put("user-agent", ua);
			headers.put("cookie", cookie);

			headers.put(":authority:", "star.toutiao.com");
			headers.put(":method:", "GET");
			headers.put(":scheme:", "http");

			String authid = r.getString("star_id");
			String nick_name = r.getString("nick_name");
			String author_page = r.getString("author_page");
			String author_order_info = r.getString("author_order_info");
			String author_daily_fans = r.getString("author_daily_fans");
			String author_fans_distribution = r.getString("author_fans_distribution");
			String author_item_data = r.getString("author_item_data");
			String author_item_info = r.getString("author_item_info");

			/**
			 * author_page
			 */
			JSONObject author_page_json = null;
			if (StringUtils.isEmpty(author_page)) {
				String author_page_url = "https://star.toutiao.com/v/api/user/author_page/?author_id=" + authid
						+ "&recommend=false&platform_source=1";

				String author_page_result = HttpRequest.get(author_page_url).header(Header.USER_AGENT, ua)
						.header(Header.COOKIE, cookie).timeout(60000)// 超时，毫秒
						.execute().body();

				try {
					author_page_json = JSONObject.parseObject(author_page_result);
					if (!author_page_json.getInteger("code").equals(0)) {
						author_page_json = null;
						logger.info(author_page_result);
					}

				} catch (Exception e) {
					logger.error("解析author_page_json异常");
				}
			}

			/**
			 * author_order_info
			 */

			JSONObject author_order_info_json = null;
			if (StringUtils.isEmpty(author_order_info)) {

				String author_order_info_url = "https://star.toutiao.com/v/api/demand/author_order_info/?author_id="
						+ authid + "&platform_source=1";

				String author_order_info_result = HttpRequest.get(author_order_info_url).header(Header.USER_AGENT, ua)
						.header(Header.COOKIE, cookie).timeout(60000)// 超时，毫秒
						.execute().body();

				try {
					author_order_info_json = JSONObject.parseObject(author_order_info_result);
					if (!author_order_info_json.getInteger("code").equals(0)) {
						author_order_info_json = null;
						logger.info(author_order_info_result);
					}
				} catch (Exception e) {
					logger.error("解析author_order_info_json异常");
				}
			}

			/**
			 * author_item_info
			 */
			JSONObject author_item_info_json = null;
			if (StringUtils.isEmpty(author_item_info)) {
				String author_item_info_url = "https://star.toutiao.com/v/api/demand/author_item_info/?author_id="
						+ authid + "&platform_source=1";

				String author_item_info_result = HttpRequest.get(author_item_info_url).header(Header.USER_AGENT, ua)
						.header(Header.COOKIE, cookie).timeout(60000)// 超时，毫秒
						.execute().body();

				try {
					author_item_info_json = JSONObject.parseObject(author_item_info_result);
					if (!author_item_info_json.getInteger("code").equals(0)) {
						author_item_info_json = null;
						logger.info(author_item_info_result);
					}
				} catch (Exception e) {
					logger.error("解析author_item_info_json异常");
				}
			}

			/**
			 * author_item_data
			 */
			JSONObject author_item_data_json = null;
			if (StringUtils.isEmpty(author_item_data)) {

				String author_item_data_url = "https://star.toutiao.com/v/api/demand/author_item_data/?author_id="
						+ authid + "&platform_source=1&item_cnt=15";

				String author_item_data_result = HttpRequest.get(author_item_data_url).header(Header.USER_AGENT, ua)
						.header(Header.COOKIE, cookie).timeout(60000)// 超时，毫秒
						.execute().body();

				try {
					author_item_data_json = JSONObject.parseObject(author_item_data_result);
					if (!author_item_data_json.getInteger("code").equals(0)) {
						author_item_data_json = null;
						logger.info(author_item_data_result);
					}
				} catch (Exception e) {
					logger.error("解析author_item_data_json异常");
				}

			}

			/**
			 * author_daily_fans
			 */
			JSONObject author_daily_fans_json = null;
			if (StringUtils.isEmpty(author_daily_fans)) {
				String author_daily_fans_url = "https://star.toutiao.com/v/api/demand/author_daily_fans/?author_id="
						+ authid + "&platform_source=1&start_date=2019-04-30&end_date=2019-07-31";

				String author_daily_fans_result = HttpRequest.get(author_daily_fans_url).header(Header.USER_AGENT, ua)
						.header(Header.COOKIE, cookie).timeout(60000)// 超时，毫秒
						.execute().body();

				try {
					author_daily_fans_json = JSONObject.parseObject(author_daily_fans_result);
					if (!author_daily_fans_json.getInteger("code").equals(0)) {
						author_daily_fans_json = null;
						logger.info(author_daily_fans_result);
					}
				} catch (Exception e) {
					logger.error("解析author_item_data_json异常");
				}

			}

			/**
			 * author_fans_distribution
			 */
			JSONObject author_fans_distribution_json = null;

			if (StringUtils.isEmpty(author_fans_distribution)) {

				String author_fans_distribution_url = "https://star.toutiao.com/v/api/demand/author_fans_distribution/?author_id="
						+ authid + "&scope=0&platform_source=1";

				String author_fans_distribution_result = HttpRequest.get(author_fans_distribution_url)
						.header(Header.USER_AGENT, ua).header(Header.COOKIE, cookie).timeout(60000)// 超时，毫秒
						.execute().body();

				try {
					author_fans_distribution_json = JSONObject.parseObject(author_fans_distribution_result);
					if (!author_fans_distribution_json.getInteger("code").equals(0)) {
						author_fans_distribution_json = null;
						logger.info(author_fans_distribution_result);
					}
				} catch (Exception e) {
					logger.error("解析author_fans_distribution_result异常");
				}
			}

			Chain chain = Chain.make("status", 2);
			if (author_page_json != null) {
				chain.add("author_page", author_page_json.toJSONString());
			}

			if (author_order_info_json != null) {
				chain.add("author_order_info", author_order_info_json.toJSONString());
			}

			if (author_item_info_json != null) {
				chain.add("author_item_info", author_item_info_json.toJSONString());
			}

			if (author_item_data_json != null) {
				chain.add("author_item_data", author_item_data_json.toJSONString());
			}

			if (author_daily_fans_json != null) {
				chain.add("author_daily_fans", author_daily_fans_json.toJSONString());
			}
			if (author_fans_distribution_json != null) {
				chain.add("author_fans_distribution", author_fans_distribution_json.toJSONString());
			}

			String fd = DateUtil.format(new Date(), "yyyyMMdd");
			Record daily = new Record();

			daily.put(".table", "t_star_auth_info_daily");
			daily.put("star_id", authid);
			daily.put("nick_name", nick_name);
			daily.put("author_page", author_page_json);
			daily.put("author_order_info", author_order_info_json);
			daily.put("author_item_info", author_item_info_json);
			daily.put("author_item_data", author_item_data_json);
			daily.put("author_daily_fans", author_daily_fans_json);
			daily.put("author_fans_distribution", author_fans_distribution_json);
			daily.put("create_time", new Date());
			daily.put("create_date", new Date());
			daily.put("cdate", fd);
			try {

				if (dao.fetch("t_star_auth_info_daily",
						Cnd.where("star_id", "=", authid).and("cdate", "=", fd)) != null) {
					dao.update("t_star_auth_info", chain, Cnd.where("star_id", "=", authid).and("cdate", "=", fd));
				} else {
					dao.fastInsert(daily);
				}
			} catch (Exception e) {
				logger.error("", e);
			}

			dao.update("t_star_auth_info", Chain.make("status", 2), Cnd.where("star_id", "=", authid));
			logger.info("#########下载完毕,auth_id:" + authid);
		} catch (Exception e) {

			logger.error("", e);
		}

	}

}
